// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten. You can (and have to!) safely modify it by hand.
 */
package org.tinaelle.server.service;

import org.tinaelle.common.dto.AdCountryDTO;
import org.tinaelle.common.dto.AdCountryTrlDTO;
import org.tinaelle.common.dto.AdCurrencyDTO;
import org.tinaelle.common.dto.AdCurrencyTrlDTO;
import org.tinaelle.common.dto.AdLanguageDTO;
import org.tinaelle.server.domain.AdCountryDao;
import org.tinaelle.server.domain.AdCurrencyDao;
import org.tinaelle.server.domain.AdLanguageDao;
import org.tinaelle.utils.BootstrapUtils;

/**
 * @see org.tinaelle.service.AdI18NService
 */
public class AdI18NServiceImpl extends org.tinaelle.server.service.AdI18NServiceBase {

	/**
     * 
     */
	private static final long	serialVersionUID	= 1L;

	/**
	 * @see org.tinaelle.service.AdI18NService#getByLanguage(java.lang.String)
	 */
	@Override
	protected org.tinaelle.common.dto.AdLanguageDTO handleGetByLanguage(final java.lang.String language)
	        throws java.lang.Exception {
		return ( AdLanguageDTO ) getAdLanguageDao().findByLanguage( AdLanguageDao.TRANSFORM_ADLANGUAGEDTO, language );
	}

	@Override
	protected AdCountryDTO handleGetCountryByCountryCode(final String countryCode) throws Exception {
		return ( AdCountryDTO ) getAdCountryDao().findByCountryCode( AdCountryDao.TRANSFORM_ADCOUNTRYDTO, countryCode );
	}

	@Override
	protected AdCurrencyDTO handleGetCurrencyByIsoCode(final String isoCode) throws Exception {
		return ( AdCurrencyDTO ) getAdCurrencyDao().findByIsoCode( AdCurrencyDao.TRANSFORM_ADCURRENCYDTO, isoCode );
	}

	@Override
	protected AdCountryTrlDTO handleGetCountryByCountryCode(final String countryCode, final AdLanguageDTO language)
	        throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected AdCurrencyTrlDTO handleGetCurrencyByIsoCode(final String isoCode, final AdLanguageDTO language)
	        throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void handleFillInitialValues() throws Exception {
		/**
		 * Load Languages
		 */
		fillInitialValuesForLanguage();

		/**
		 * Load Currencies
		 */
		fillInitialValuesForCurrency();

		/**
		 * Load Countries
		 */
		fillInitialValuesForCountry();
	}

	@Override
	protected void handleFillInitialValuesForCountry() throws Exception {
		// File Format :
		// NAME, DESCRIPTION, COUNTRYCODE, : 0, 1, 2
		// HASREGION, REGIONNAME, EXPRESSIONPHONE, : 3, 4, 5
		// DISPLAYSEQUENCE, EXPRESSIONPOSTAL, HASPOSTAL_ADD, : 6, 7, 8
		// EXPRESSIONPOSTAL_ADD, AD_LANGUAGE, CURRENCY ISO CODE, : 9, 10, 11
		// DISPLAYSEQUENCELOCAL, ISADDRESSLINESREVERSE, ISADDRESSLINESLOCALREVERSE, : 12, 13, 14
		// EXPRESSIONBANKROUTINGNO, EXPRESSIONBANKACCOUNTNO, MEDIASIZE : 15, 16, 17
		String[][] countryContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdCountry" );
		for ( int i = 1; i < countryContent.length; i++ ) {
			final String[] countryLine = countryContent[ i ];

			if ( countryLine.length > 14 ) {
				AdCountryDTO adCountryDTO = getCountryByCountryCode( countryLine[ 2 ] );
				if ( adCountryDTO == null ) {
					adCountryDTO = new AdCountryDTO();
				}
				adCountryDTO.setName( countryLine[ 0 ] );
				adCountryDTO.setDescription( countryLine[ 1 ] );
				adCountryDTO.setCountryCode( countryLine[ 2 ] );
				adCountryDTO.setHasRegion( countryLine[ 3 ].trim().equalsIgnoreCase( "TRUE" )
				        || countryLine[ 3 ].trim().equalsIgnoreCase( "Y" )
				        || countryLine[ 3 ].trim().equalsIgnoreCase( "1" ) );
				adCountryDTO.setRegionName( countryLine[ 4 ] );
				adCountryDTO.setExpressionPhone( countryLine[ 5 ] );
				adCountryDTO.setDisplaySequence( countryLine[ 6 ] );
				adCountryDTO.setExpressionPostal( countryLine[ 7 ] );
				adCountryDTO.setHasPostalAdd( countryLine[ 8 ].trim().equalsIgnoreCase( "TRUE" )
				        || countryLine[ 8 ].trim().equalsIgnoreCase( "Y" )
				        || countryLine[ 8 ].trim().equalsIgnoreCase( "1" ) );
				adCountryDTO.setExpressionPostalAdd( countryLine[ 9 ] );
				if ( countryLine[ 10 ].trim().length() > 0 ) {
					adCountryDTO.setAdLanguageId( handleGetByLanguage( countryLine[ 10 ].trim() ).getAdLanguageId() );
				}
				if ( countryLine[ 11 ].trim().length() > 0 ) {
					System.out.println( "Currency ISO : " + countryLine[ 11 ].trim() );
					adCountryDTO.setAdCurrencyId( handleGetCurrencyByIsoCode( countryLine[ 11 ].trim() )
					        .getAdCurrencyId() );
				}
				adCountryDTO.setDisplaySequenceLocal( countryLine[ 12 ] );
				adCountryDTO.setIsAddressLineReverse( countryLine[ 13 ].trim().equalsIgnoreCase( "TRUE" )
				        || countryLine[ 13 ].trim().equalsIgnoreCase( "Y" )
				        || countryLine[ 13 ].trim().equalsIgnoreCase( "1" ) );
				adCountryDTO.setIsAddressLineLocalReverse( countryLine[ 14 ].trim().equalsIgnoreCase( "TRUE" )
				        || countryLine[ 14 ].trim().equalsIgnoreCase( "Y" )
				        || countryLine[ 14 ].trim().equalsIgnoreCase( "1" ) );
				if ( countryLine.length == 16 ) {
					adCountryDTO.setMediaSize( countryLine[ 15 ] );
				}
				adCountryDTO = save( adCountryDTO );
			}
		}
	}

	@Override
	protected void handleFillInitialValuesForCurrency() throws Exception {
		// File Format :
		// ISO_CODE, CURSYMBOL, DESCRIPTION, : 0, 1, 2
		// STDPRECISION, COSTINGPRECISION, ISEURO, : 3, 4, 5
		// ISEMUMEMBER, EMUENTRYDATE, EMURATE : 6, 7, 8
		String[][] currencyContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdCurrency" );
		for ( int i = 1; i < currencyContent.length; i++ ) {
			final String[] currencyLine = currencyContent[ i ];

			if ( currencyLine.length > 6 ) {
				AdCurrencyDTO adCurrencyDTO = getCurrencyByIsoCode( currencyLine[ 0 ] );
				if ( adCurrencyDTO == null ) {
					adCurrencyDTO = new AdCurrencyDTO();
				}
				adCurrencyDTO.setIsoCode( currencyLine[ 0 ] );
				adCurrencyDTO.setCurrencySymbol( currencyLine[ 1 ] );
				adCurrencyDTO.setDescription( currencyLine[ 2 ] );
				adCurrencyDTO.setDecimalPrecision( Integer.parseInt( currencyLine[ 3 ] ) );

				adCurrencyDTO = save( adCurrencyDTO );
			}
		}
	}

	@Override
	protected void handleFillInitialValuesForLanguage() throws Exception {
		// File Format :
		// AD_LANGUAGE, NAME, LANGUAGEISO, COUNTRYCODE, : 0, 1, 2, 3
		// ISBASELANGUAGE, ISSYSTEMLANGUAGE, ISDECIMALPOINT, : 4, 5, 6
		// DATEPATTERN, TIMEPATTERN : 7, 8
		String[][] languageContent = BootstrapUtils.instance().getBootstrapSheetContent( "AdLanguage" );
		for ( int i = 1; i < languageContent.length; i++ ) {
			final String[] languageLine = languageContent[ i ];

			if ( languageLine.length > 6 ) {
				AdLanguageDTO adLanguageDTO = handleGetByLanguage( languageLine[ 0 ] );
				if ( adLanguageDTO == null ) {
					adLanguageDTO = new AdLanguageDTO();
				}
				adLanguageDTO.setLanguage( languageLine[ 0 ] );
				adLanguageDTO.setName( languageLine[ 1 ] );
				adLanguageDTO.setLanguageIso( languageLine[ 2 ] );
				adLanguageDTO.setCountryCode( languageLine[ 3 ] );
				adLanguageDTO.setIsBaseLanguage( languageLine[ 4 ].trim().equalsIgnoreCase( "TRUE" )
				        || languageLine[ 4 ].trim().equalsIgnoreCase( "Y" )
				        || languageLine[ 4 ].trim().equalsIgnoreCase( "1" ) );
				adLanguageDTO.setIsSystemLanguage( languageLine[ 5 ].trim().equalsIgnoreCase( "TRUE" )
				        || languageLine[ 5 ].trim().equalsIgnoreCase( "Y" )
				        || languageLine[ 5 ].trim().equalsIgnoreCase( "1" ) );
				adLanguageDTO.setIsDecimalPoint( languageLine[ 6 ].trim().equalsIgnoreCase( "TRUE" )
				        || languageLine[ 6 ].trim().equalsIgnoreCase( "Y" )
				        || languageLine[ 6 ].trim().equalsIgnoreCase( "1" ) );
				if ( languageLine.length > 7 ) {
					adLanguageDTO.setDatePattern( languageLine[ 7 ] );
					adLanguageDTO.setTimePattern( languageLine[ 8 ] );
				}
				adLanguageDTO = save( adLanguageDTO );
			}
		}
	}
}